//var gameHandle = require('../handler/handle');
var async = require("async")
var openRoomLogger = require("pomelo-logger").getLogger("openRoom-log");
var game_managerLogger = require("pomelo-logger").getLogger("game_manager-log");
var errorLogger = require("pomelo-logger").getLogger("error-log");
var competitionCfg = require("../match/cfg/competition.json")
var http = require('http')
var ROOM_ALL_AMOUNT = 899000			//总房间数量
var ROOM_BEGIN_INDEX = 100800   	    //起始房间ID
var maxCheckpoint = 7 				    //最大闯关数

module.exports = function(app) {
	return new GameRemote(app);
};
var local = {}
var GameRemote = function(app) {
	var self = this
	self.app = app
	GameRemote.app = app
	GameRemote.channelService = this.app.get('channelService');
	GameRemote.NodeNumber = 0
	//玩家所在连接服务器映射
	GameRemote.userConnectorMap = {}
	//玩家所在房间映射
	GameRemote.userRoomMap = {}
	//房间内玩家列表
	GameRemote.roomUserList = {}
	//房间所在服务器节点映射
	GameRemote.roomNodeMap = {}
	GameRemote.matchManager = self.app.get("matchManager")
	GameRemote.lobbyManager = self.app.get("lobbyManager")
	GameRemote.solitaryManager = self.app.get("solitaryManager")
    GameRemote.dbService = self.app.get("dbService")
    if(GameRemote.dbService && GameRemote.dbService.db){
    	GameRemote.db = GameRemote.dbService.db
    }
    //维护状态
    GameRemote.maintainFlag = false
};
GameRemote.mainTimer = function() {
	GameRemote.matchManager.mainTimer()
}
//用户连接
GameRemote.prototype.userConnect = function(uid,sid,cb) {
	if(GameRemote.userConnectorMap[uid]){
		var notify = {
			cmd : "repeatLogin"
		}
		var params = {}
		params.cid = GameRemote.userConnectorMap[uid]
		GameRemote.app.rpc.connector.remote.sendByUid(null,params,uid,notify,function(){
			if(cb){
				cb()
			}
		})
	}else{
		if(cb){
			cb()
		}
	}
	GameRemote.userConnectorMap[uid] = sid
}
GameRemote.prototype.recover = function(cb) {
	if(GameRemote.matchManager){
		GameRemote.matchManager.setRemote(GameRemote)
		GameRemote.lobbyManager.setRemote(GameRemote)
		GameRemote.solitaryManager.setRemote(GameRemote)
		GameRemote.matchManager.init()
		GameRemote.lobbyManager.init()
		GameRemote.solitaryManager.init()
    	setInterval(function() {
    		setTimeout(GameRemote.mainTimer,1)
    	},200)
	}
	cb(true)
}

//客户端指令
GameRemote.prototype.receive = function(uid, sid,code,params,cb) {
	var self = this
	if(GameRemote.userConnectorMap[uid] != sid){
		cb(false,"已被踢出")
		return
	}
	// console.log("uid:"+uid)
	// console.log("sid:"+sid)
	// console.log("code:"+code)
	// console.log(params)
	switch(code){
		case "newRoom":
			if(GameRemote.maintainFlag){
				cb(false,"游戏正在维护中")
				return
			}
			//新房间
			GameRemote.newRoom(uid,sid,params,cb)
		return
		case "join":
			if(GameRemote.maintainFlag){
				cb(false,"游戏正在维护中")
				return
			}
			//加入房间
			local.join(uid,sid,params,cb)
		return
		case "joinLobbyRoom":
			//加入大厅房间
			if(GameRemote.maintainFlag){
				cb(false,"游戏正在维护中")
				return
			}
			var gameType = params.gameType
			var level = params.level
			var table = params.table
			if(!gameType || !level || typeof(table) != "number"){
				cb(false,"参数错误")
				return
			}
			GameRemote.lobbyManager.joinLobbyRoom(uid,sid,gameType,level,table,cb)
		return
		case "getLobbyInfo":
			//获取大厅房间数据
			var gameType = params.gameType
			var level = params.level
			GameRemote.lobbyManager.getLobbyInfo(gameType,level,cb)
		return
		case "solitary":
			var type = params.type
			if(GameRemote.solitaryManager[type] && typeof(GameRemote.solitaryManager[type]) == "function"){
				GameRemote.solitaryManager[type](uid,params,cb)
			}else{
				cb(false,"接口不存在")
			}
		return
		case "lockingMachine":
			//锁定机器
			var roomId = GameRemote.userRoomMap[uid]
			var type = params.type
			GameRemote.lobbyManager.lockingMachine(uid,roomId,type,cb)
		break
		default:
			if(GameRemote.userRoomMap[uid] !== undefined){
				var roomId = GameRemote.userRoomMap[uid]
				if(!params){
					params = {}
				}
				params.gid = GameRemote.roomNodeMap[roomId]
				self.app.rpc.gameNode.remote.receive(null,params,uid,sid,roomId,code,function (flag,msg){
					cb(flag,msg)
				})
			}else{
				cb(false)
			}
	}
}
//创建新房间
GameRemote.newRoom = function(uid,sid,params,cb) {
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	async.waterfall([
		function(next) {
			//获取玩家信息
			GameRemote.app.rpc.db.remote.getPlayerInfoByUid(null,uid,function(data) {
				next(null,data)
			})
		},
		function(playerInfo) {
			//找到空闲房间ID
			params.playerInfo = playerInfo
			var roomId = local.getUnusedRoom()
			if(roomId !== false){
				//分配游戏服务器
				GameRemote.NodeNumber++
				var nodeLength = GameRemote.app.getServersByType('gameNode').length
				if(GameRemote.NodeNumber >= nodeLength){
					GameRemote.NodeNumber = 0
				}
				//记录房间对应游戏服务器
				GameRemote.roomNodeMap[roomId] = GameRemote.NodeNumber
				params.gid = GameRemote.roomNodeMap[roomId]
				//与游戏服务器连接
				GameRemote.app.rpc.gameNode.remote.newRoom(null,params,uid,sid,roomId,function (flag,msg) {
					if(flag === true){
						openRoomLogger.info("new room : "+roomId)
						GameRemote.userRoomMap[uid] = roomId;
						GameRemote.roomUserList[roomId] = []
						var info = {
							"uid" : playerInfo.uid,
							"nickname" : playerInfo.nickname,
							"head" : playerInfo.head
						}
						GameRemote.roomUserList[roomId].push(info)
					}else{
						delete GameRemote.roomNodeMap[roomId]
					}
					cb(flag,msg)
				})
			}else{
				cb(false,{"code" : "房间已满"})
			}
		}
		], function (err, result) {
		console.log(err)
		cb(false)
		return
	});
}
//大厅房间创建
GameRemote.newLobbyRoom = function(playerInfo,uid,sid,gameType,level,table,basicScore,cb) {
	if(GameRemote.userRoomMap[uid]){
		console.log("已在房间中")
		cb(false)
		return
	}
	var params = {}
	params.playerInfo = playerInfo
	params.gameType = gameType
	params.level = level
	params.table = table
	params.basicScore = basicScore
	var roomId = local.getUnusedRoom()
	if(roomId !== false){
		//分配游戏服务器
		GameRemote.NodeNumber++
		var nodeLength = GameRemote.app.getServersByType('gameNode').length
		if(GameRemote.NodeNumber >= nodeLength){
			GameRemote.NodeNumber = 0
		}
		//记录房间对应游戏服务器
		GameRemote.roomNodeMap[roomId] = GameRemote.NodeNumber
		params.gid = GameRemote.roomNodeMap[roomId]
		//与游戏服务器连接
		GameRemote.app.rpc.gameNode.remote.newRoom(null,params,uid,sid,roomId,function (flag,msg) {
			if(flag === true){
				openRoomLogger.info("new room : "+roomId)
				GameRemote.userRoomMap[uid] = roomId;
				GameRemote.roomUserList[roomId] = []
				var info = {
					"uid" : playerInfo.uid,
					"nickname" : playerInfo.nickname,
					"head" : playerInfo.head,
					"gameType" : gameType,
					"level" : level,
					"table" : table
				}
				GameRemote.roomUserList[roomId].push(info)
				cb(roomId)
				return
			}else{
				GameRemote.lobbyManager.lobbyRoomOver(roomId)
				delete GameRemote.roomNodeMap[roomId]
				cb(false)
				return
			}
		})
	}else{
		cb(false)
		return
	}
}
//加入房间
local.join = function(uid,sid,params,cb) {
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	if(!params || typeof(params.roomId) != "number" || params.roomId < 0 || GameRemote.roomNodeMap[params.roomId] === undefined){
        cb(false,"房间号错误")
        return
	}
	async.waterfall([
		function(next) {
			//获取玩家信息
			GameRemote.app.rpc.db.remote.getPlayerInfoByUid(null,uid,function(data) {
				next(null,data)
			})
		},
		function(playerInfo) {
			//房间ID
			params.playerInfo = playerInfo
			var roomId = params.roomId
			params.gid = GameRemote.roomNodeMap[roomId]
			GameRemote.app.rpc.gameNode.remote.join(null,params,uid,sid,roomId,function(flag,msg) {
				if(flag){
					GameRemote.userRoomMap[uid] = roomId;
					var info = {
						"uid" : playerInfo.uid,
						"nickname" : playerInfo.nickname,
						"head" : playerInfo.head
					}
					GameRemote.roomUserList[roomId].push(info)
				}
				cb(flag,msg)
			})
		}
		], function (err, result) {
		console.log(err)
		cb(false)
		return
	});
}
//玩家离线
GameRemote.prototype.kick = function(uid,sid,cb) {
	if(GameRemote.userConnectorMap[uid] && GameRemote.userConnectorMap[uid] == sid){
		console.log("user leave : "+uid)
		delete GameRemote.userConnectorMap[uid]
		local.leaveMatching(uid,function(){})
		if(GameRemote.userRoomMap[uid] != undefined){
			var roomId = GameRemote.userRoomMap[uid]
			var params = {}
			params.gid = GameRemote.roomNodeMap[roomId]
			this.app.rpc.gameNode.remote.disconnect(null,params,uid,null,roomId,function (flag){
				cb(flag)
			})
		}else{
			if(cb){
				cb(false)
			}
		}
	}else{
		if(cb){
			cb(false)
		}
	}
};
//玩家重连
GameRemote.prototype.reconnection = function(uid,sid,cb) {
	var data = {}
	data.lockinfo = GameRemote.lobbyManager.getLockinfo(uid)
	if(GameRemote.userRoomMap[uid] !== undefined){
		var roomId = GameRemote.userRoomMap[uid]
		if(roomId != undefined && GameRemote.roomNodeMap[roomId] != undefined){
			var params = {}
			params.gid = GameRemote.roomNodeMap[roomId]
			this.app.rpc.gameNode.remote.reconnection(null,params,uid,sid,roomId,function (flag){
				if(flag == false){
					delete GameRemote.userRoomMap[uid]
					data.state = GameRemote.matchManager.getCompetitionState(uid)
				}else if(flag == true){
					data.state = "inGame"
				}
				cb(data)
			})
		}else{
			delete GameRemote.userRoomMap[uid]
			data.state = GameRemote.matchManager.getCompetitionState(uid)
			cb(data)
		}
	}else{
		data.state = GameRemote.matchManager.getCompetitionState(uid)
		cb(data)
	}
}
//玩家退出房间
GameRemote.prototype.userQuit = function(uid,roomId,cb) {
	delete GameRemote.userRoomMap[uid]
	if(roomId && GameRemote.roomUserList[roomId]){
		for(var i = 0;i < GameRemote.roomUserList[roomId].length;i++){
			if(GameRemote.roomUserList[roomId][i].uid == uid){
				GameRemote.roomUserList[roomId].splice(i,1)
				break
			}
		}
	}
	GameRemote.matchManager.userQuit(roomId,uid)
	cb(true)
}
//向玩家发送消息
GameRemote.prototype.sendByUid = function(uid,notify,cb) {
	var params = {}
	params.cid = GameRemote.userConnectorMap[uid]
	if(params.cid){
		GameRemote.app.rpc.connector.remote.sendByUid(null,params,uid,notify,function(){})
	}
	cb(true)
}
//广播发送消息
GameRemote.prototype.broadcast = function(notify,cb) {
	GameRemote.broadcast(notify)
	cb(true)
}
GameRemote.broadcast = function(notify) {
	GameRemote.channelService.broadcast("connector","onNotify",notify)
}
GameRemote.sendByUid = function(uid,notify) {
	var params = {}
	params.cid = GameRemote.userConnectorMap[uid]
	if(params.cid){
		GameRemote.app.rpc.connector.remote.sendByUid(null,params,uid,notify,function(){})
	}
}
//房间结束
GameRemote.prototype.gameOver = function(roomId,info,cb){
	for(var i in GameRemote.roomUserList[roomId]){
		delete GameRemote.userRoomMap[GameRemote.roomUserList[roomId][i].uid]
	}
	delete GameRemote.roomUserList[roomId]
	delete GameRemote.roomNodeMap[roomId]
	if(cb){
		cb()
	}
	GameRemote.matchManager.matchRoomOver(roomId,info)
	GameRemote.lobbyManager.lobbyRoomOver(roomId,info)
}
//-------------------------------------房间号管理-------------------------------------//
//随机分配房间号
local.getUnusedRoom = function() {
	var roomId = Math.floor((Math.random() * ROOM_ALL_AMOUNT))
	for(var i = roomId;i < ROOM_ALL_AMOUNT + roomId;i++){
		var index = (i % ROOM_ALL_AMOUNT) + ROOM_BEGIN_INDEX
		if(!GameRemote.roomNodeMap[index]){
			return index
		}
	}
	return false
}
//-------------------------------------管理员相关--------------------------------------//
//获取房间列表
GameRemote.prototype.getRoomList = function(cb) {
	console.log("GameRemote.roomUserList")
	cb(GameRemote.roomUserList)
}
//获取房间详细信息列表
GameRemote.prototype.getRoomInfo = function(roomId,cb) {
	var params = {}
	params.gid = GameRemote.roomNodeMap[roomId]
	if(params.gid === undefined){
		cb(false)
	}else{
		GameRemote.app.rpc.gameNode.remote.getRoomInfo(null,params,roomId,cb)
	}
}
//设置维护
GameRemote.prototype.setMaintainFlag = function(flag,cb) {
	if(flag === true){
		GameRemote.maintainFlag = true
	}else{
		GameRemote.maintainFlag = false
	}
	GameRemote.channelService.broadcast("connector","onNotify",{"type":"setMaintainFlag","flag" : flag})
	cb(true)
}
//获取在线人数
GameRemote.prototype.getOnlineUser = function(cb) {
	cb(GameRemote.userConnectorMap)
}
//-------------------------------------匹配相关--------------------------------------//
//加入匹配
GameRemote.prototype.joinMatching = function(uid,type,cb) {
	if(GameRemote.maintainFlag){
		cb(false,"游戏正在维护中")
		return
	}
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	//前置条件判断
	switch(type){
		case "hunanRunQuicklyCheckpoint":
			this.app.rpc.db.hunanRunQuickly.getCheckpointInfo(null,uid,function(flag,msg) {
				if(msg && msg.playCount >= 0 && msg.winCount < maxCheckpoint){
					local.joinMatching(uid,type,{winCount : msg.winCount},cb)
				}else{
					cb(false)
					return
				}
			})
		break
		default:
			local.joinMatching(uid,type,{},cb)
	}
}
//离开匹配
GameRemote.prototype.leaveMatching = function(uid,cb) {
	local.leaveMatching(uid,cb)
}
//加入匹配
local.joinMatching = function(uid,type,info,cb) {
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	GameRemote.app.rpc.db.remote.getPlayerInfoByUid(null,uid,function(data) {
		if(!data){
			cb(false)
			return
		}
		data.sid = GameRemote.userConnectorMap[uid]
		GameRemote.matchManager.joinMatching(data,type,info,cb)
	})
}
//离开匹配
local.leaveMatching = function(uid,cb) {
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	GameRemote.matchManager.leaveMatching(uid,cb)
}
//匹配成功，整组玩家加入房间
GameRemote.groupNewRoom = function(gameType,matchType,players,info) {
	console.log("groupNewRoom!!")
	var roomId = local.getUnusedRoom()
	if(roomId !== false){
		roomId = roomId * 100 + 88
		//分配游戏服务器
		GameRemote.NodeNumber++
		var nodeLength = GameRemote.app.getServersByType('gameNode').length
		if(GameRemote.NodeNumber >= nodeLength){
			GameRemote.NodeNumber = 0
		}
		//记录房间对应游戏服务器
		GameRemote.roomNodeMap[roomId] = GameRemote.NodeNumber
		var params = {}
		params.gid = GameRemote.roomNodeMap[roomId]
		params.gameType = gameType
		GameRemote.roomUserList[roomId] = []
		for(var i = 0;i < players.length;i++){
			if(players[i].uid > 10000){
				var data = {
					"uid" : players[i].uid,
					"nickname" : players[i].nickname,
					"head" : players[i].head
				}
				GameRemote.roomUserList[roomId].push(data)
				GameRemote.userRoomMap[players[i].uid] = roomId;
			}
		}
		//与游戏服务器连接
		GameRemote.app.rpc.gameNode.remote.groupNewRoom(null,params,players,roomId,info,function (flag,msg) {
			if(flag === true){
				openRoomLogger.info("groupNewRoom room : "+roomId)
				local.groupNewRoomSuccess(roomId,matchType,players)
			}else{
				for(var i in GameRemote.roomUserList[roomId]){
					delete GameRemote.userRoomMap[GameRemote.roomUserList[roomId][i].uid]
				}
				delete GameRemote.roomUserList[roomId]
				delete GameRemote.roomNodeMap[roomId]
				local.groupNewRoomFail(players)
			}
		})
	}else{
		local.groupNewRoomFail(players)
	}
}
//匹配成功且创建房间成功
local.groupNewRoomSuccess = function(roomId,matchType,players) {
	console.log("groupNewRoomSuccess!!! : "+matchType)
	var notify = {
		cmd : "groupNewRoomSuccess"
	}
	for(var i = 0;i < players.length;i++){
		if(players[i].uid > 10000){
			GameRemote.prototype.sendByUid(players[i].uid,notify,function(){})
		}
	}
	GameRemote.matchManager.matchRoomBegin(roomId,matchType,players)
}
//匹配成功且加入房间失败
local.groupNewRoomFail = function(players) {
	console.log("groupNewRoomFail!!!")
	var notify = {
		cmd : "groupNewRoomFail"
	}
	for(var i = 0;i < players.length;i++){
		if(players[i].uid > 10000){
			GameRemote.prototype.sendByUid(players[i].uid,notify,function(){})
		}
	}
}
//匹配旧房间
GameRemote.mathchJoin = function(roomId,player,cb) {
	var uid = player.uid
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	if(typeof(roomId) != "number" || roomId < 0 || GameRemote.roomNodeMap[roomId] === undefined){
        cb(false,"房间号错误")
        return
	}
	//房间ID
	var params = {}
	params.playerInfo = player
	params.gid = GameRemote.roomNodeMap[roomId]
	GameRemote.app.rpc.gameNode.remote.join(null,params,uid,GameRemote.userConnectorMap[uid],roomId,function(flag,msg) {
		if(flag){
			GameRemote.userRoomMap[uid] = roomId;
			var info = {
				"uid" : player.uid,
				"nickname" : player.nickname,
				"head" : player.head
			}
			GameRemote.roomUserList[roomId].push(info)
			var notify = {
				cmd : "groupNewRoomSuccess"
			}
			GameRemote.prototype.sendByUid(player.uid,notify,function(){})
		}
		cb(flag,msg)
	})
}
//获取金币场玩家数量
GameRemote.prototype.getGoldMatchPlyerCounts = function(cb){
	var list = GameRemote.matchManager.getGoldMatchPlyerCounts()
	cb(true,list)
}
//报名比赛场
GameRemote.prototype.signUp = function(uid,cid,cb) {
	if(GameRemote.maintainFlag){
		cb(false,"游戏正在维护中")
		return
	}
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	GameRemote.matchManager.signUp(uid,cid,cb)
}
//比赛场小局比赛开始
GameRemote.groupNewCompetitionRoom = function(numeration,RoomIndex,curRound,maxRound,players,gameType,roomId,cid) {
	for(var i = 0;i < players.length;i++){
		players[i].sid = GameRemote.userConnectorMap[players[i].uid]
	}
	console.log("groupNewCompetitionRoom!!")
	//分配游戏服务器
	GameRemote.NodeNumber++
	var nodeLength = GameRemote.app.getServersByType('gameNode').length
	if(GameRemote.NodeNumber >= nodeLength){
		GameRemote.NodeNumber = 0
	}
	//记录房间对应游戏服务器
	GameRemote.roomNodeMap[roomId] = GameRemote.NodeNumber
	var params = {}
	params.gid = GameRemote.roomNodeMap[roomId]
	params.gameType = gameType
	GameRemote.roomUserList[roomId] = []
	for(var i = 0;i < players.length;i++){
		if(players[i].uid > 10000){
			var data = {
				"uid" : players[i].uid,
				"nickname" : players[i].nickname,
				"head" : players[i].head
			}
			GameRemote.roomUserList[roomId].push(data)
			GameRemote.userRoomMap[players[i].uid] = roomId;
		}
	}
	var info = {
		numeration : numeration,
		RoomIndex : RoomIndex,
		curRound : curRound,
		maxRound : maxRound,
		cid : cid,
		importance : competitionCfg[cid].importance
	}
	//与游戏服务器连接
	GameRemote.app.rpc.gameNode.remote.groupNewCompetitionRoom(null,params,players,roomId,info,function (flag,msg) {
		if(flag === true){
			var notify = {
				cmd : "groupNewCompetitionRoom",
				curRound : curRound,
				gameType : gameType
			}
			for(var i = 0;i < players.length;i++){
				if(players[i].uid > 10000){
					GameRemote.prototype.sendByUid(players[i].uid,notify,function(){})
				}
			}
		}else{
			console.log(new Error("groupNewCompetitionRoom fail"))
			errorLogger.info("groupNewCompetitionRoom fail!!!")
		}
	})
}
//取消报名
GameRemote.prototype.cancelSignUp = function(uid,cb) {
	if(GameRemote.userRoomMap[uid]){
		cb(false,"玩家已在房间中")
		return
	}
	GameRemote.matchManager.cancelSignUp(uid,cb)
}
GameRemote.prototype.getCompetitionList = function(cb) {
	GameRemote.matchManager.getCompetitionList(cb)
}
GameRemote.prototype.getCompetitionInfo = function(uid,cb) {
	GameRemote.matchManager.getCompetitionInfo(uid,cb)
}
GameRemote.prototype.getCompetitionInfoByNumeration = function(numeration,cb) {
	GameRemote.matchManager.getCompetitionInfoByNumeration(numeration,cb)
}
GameRemote.prototype.getCompetitionAll = function(cb) {
	GameRemote.matchManager.getCompetitionAll(cb)
}
//获取比赛免费场参与次数
GameRemote.prototype.getFreePlayCount = function(uid,cb) {
	var playCount = GameRemote.matchManager.getFreePlayCount(uid)
	cb(true,{"playCount" : playCount})
}
//获取比赛场库存
GameRemote.prototype.getCompetitionInventory = function(cb) {
	GameRemote.dbService.db.hgetall("game:competition:inventory",function(err,data) {
		if(err || !data){
			cb(false)
		}else{
			cb(true,data)
		}
	})
}
//设置比赛场库存
GameRemote.prototype.setCompetitionInventory = function(cid,value,cb) {
	GameRemote.dbService.db.hincrbyfloat("game:competition:inventory",cid,value,function(err,data) {
		if(err){
			cb(false)
		}else{
			cb(true,data)
		}
	})
}
//获取金币场库存
GameRemote.prototype.getGoldInventory = function(cb) {
	GameRemote.dbService.db.hgetall("game:gold:inventory",function(err,data) {
		if(err || !data){
			cb(false)
		}else{
			cb(true,data)
		}
	})
}
//设置金币场库存
GameRemote.prototype.setGoldInventory = function(rid,value,cb) {
	GameRemote.dbService.db.hincrbyfloat("game:gold:inventory",rid,value,function(err,data) {
		if(err){
			cb(false)
		}else{
			cb(true,data)
		}
	})
}